#!/bin/bash
set -e

#-----------------------------------------------------------------------
function  check_source()
{
	sinfo "检查源码.. [开始]"
	if [ ! -d ${MODSRC_PATH} ]; then
		if [ -f ${MODPKG_PATH} ]; then
			cd ${MODPKG_ROOT}
			if [ "${MODPKG_NAME##*.}"x = "gz"x ]; then
				sinfo "解压 .gz  压缩包: ${MODPKG_NAME} [开始]"
				tar xzf ${MODPKG_PATH} -C ${MODSRC_ROOT}
				if [ $? -eq 0 ]; then
					sdone "解压 .gz  压缩包: ${MODPKG_NAME} [完成]"
				else
					serro "解压 .gz  压缩包: ${MODPKG_NAME} [失败]"
					cd -> /dev/null
					exit 1
				fi
			elif [ "${MODPKG_NAME##*.}"x = "bz2"x ]; then
				sinfo "解压 .bz2  压缩包: ${MODPKG_NAME} [开始]"
				tar xjf ${MODPKG_PATH} -C ${MODSRC_ROOT}
				if [ $? -eq 0 ]; then
					sdone "解压 .bz2  压缩包: ${MODPKG_NAME} [完成]"
				else
					serro "解压 .bz2  压缩包: ${MODPKG_NAME} [失败]"
					cd -> /dev/null
					exit 1
				fi
			elif [ "${MODPKG_NAME##*.}"x = "xz"x ]; then
				sinfo "解压 .xz  压缩包: ${MODPKG_NAME} [开始]"
				tar xf ${MODPKG_PATH} -C ${MODSRC_ROOT}
				if [ $? -eq 0 ]; then
					sdone "解压 .xz  压缩包: ${MODPKG_NAME} [完成]"
				else
					serro "解压 .xz  压缩包: ${MODPKG_NAME} [失败]"
					cd -> /dev/null
					exit 1
				fi
			else
				serro "无法识别的压缩格式 [失败]"
				cd -> /dev/null
				exit 1
			fi
			cd -> /dev/null
		else
			serro "没有找到压缩包: ${MODPKG_NAME} [失败]"
			exit 1
		fi
	else
		sdone "检查源码... [完成]"
	fi
}
#-----------------------------------------------------------------------
function pack_module()
{
	if [ ! -d  ${MODSRC_PATH} ]; then
		serro "没有找到源码: ${MODSRC_PATH}"
		exit 1
	fi
	cd ${MODSRC_ROOT}
	if [ "${MODPKG_NAME##*.}"x = "gz"x ]; then
		sinfo "压缩 .gz  压缩包: ${MODPKG_NAME} [开始]"
		tar czvf  ./${MODPKG_NAME} ${MODSRC_NAME}
		if [ $? -eq 0 ]; then
			sdone "压缩 .gz  压缩包: ${MODPKG_NAME} [完成]"
		else
			serro "压缩 .gz  压缩包: ${MODPKG_NAME} [失败]"
			cd -> /dev/null
			exit 1
		fi
	elif [ "${MODPKG_NAME##*.}"x = "bz2"x ]; then
		sinfo "压缩 .bz2  压缩包: ${MODPKG_NAME} [开始]"
		tar cjvf  ./${MODPKG_NAME} ${MODSRC_NAME}
		if [ $? -eq 0 ]; then
			sdone "压缩 .bz2  压缩包: ${MODPKG_NAME} [完成]"
		else
			serro "压缩 .bz2  压缩包: ${MODPKG_NAME} [失败]"
			cd -> /dev/null
			exit 1
		fi
	elif [ "${MODPKG_NAME##*.}"x = "xz"x ]; then
		sinfo "压缩 .xz  压缩包: ${MODPKG_NAME} [开始]"
		tar cf  ./${MODPKG_NAME} ${MODSRC_NAME}
		if [ $? -eq 0 ]; then
			sdone "压缩 .xz  压缩包: ${MODPKG_NAME} [完成]"
		else
			serro "压缩 .xz  压缩包: ${MODPKG_NAME} [失败]"
			cd -> /dev/null
			exit 1
		fi
	else
		serro "无法识别的压缩格式 [失败]"
		cd -> /dev/null
		exit 1
	fi

	sinfo "打包源码 [开始]"
	mv ./${MODPKG_NAME} ${MODPKG_PATH}
	if [ $? -eq 0 ];then
		sdone "打包源码 [完成]"
		cd - > /dev/null
	else
		serro "打包源码 [失败]"
		cd - > /dev/null
		exit 1
	fi
}
#-----------------------------------------------------------------------
